﻿using System;
using System.Collections.Generic;
using System.Globalization;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;
using Monkey.Warrenty.Enum;

namespace Monkey.Warrenty.Biz
{
    public class WarrantyProductBiz
    {
        #region Properties

        private readonly IWarrantyProductDao _warrantyProductDao;
        private readonly IEmployeeDao _employeeDao;
        private readonly ISupplierDao _supplierDao;

        #endregion

        #region Constructors

        public WarrantyProductBiz()
        {
            _warrantyProductDao = new WarrantyProductDao();
            _employeeDao = new EmployeeDao();
            _supplierDao = new SupplierDao();
        }

        #endregion

        #region Medthods

        public List<Supplier> GetSuppliers()
        {
            return _supplierDao.GetAllSuppliers();
        }

        public List<Employee> GetEmployees()
        {
            return _employeeDao.GetAllEmployees();
        }

        public List<WarrantyTicketProductView> GetReceivedWarrantyProductsByFilter(int startIndex, int pageSize, string sorting, string name, string code, string ticketCode)
        {
            return _warrantyProductDao.GetReceivedWarrantyProductsByFilter(startIndex, pageSize, sorting, name, code, ticketCode);
        }

        public int GetReceivedWarrantyProductsCountByFilter(string name, string code, string ticketCode)
        {
            return _warrantyProductDao.GetReceivedWarrantyProductsCountByFilter(name, code, ticketCode);
        }

        public List<WarrantyTicketProductView> GetWaitingWarrantyProducts(int startIndex, int pageSize, string sorting, string name, string code, string ticketCode)
        {
            return _warrantyProductDao.GetWarrantyTicketProductsByFilter(WarrantyProductStatus.WaitingWarranty.GetHashCode(), startIndex, pageSize, sorting, name, code, ticketCode);
        }

        public List<WarrantyTicketLendProductView> GetLendProducts(int startIndex, int pageSize, string sorting)
        {
            return _warrantyProductDao.GetLendProducts(LendProductStatus.Lending.GetHashCode(), startIndex, pageSize, sorting);
        }

        public int GetWaitingWarrantyProductsCount(string name, string code, string ticketCode)
        {
            return _warrantyProductDao.GetWarrantyProductsCountByFilter(WarrantyProductStatus.WaitingWarranty.GetHashCode(), name, code, ticketCode);
        }

        public int GetLendProductsCount()
        {
            return _warrantyProductDao.GetLendProductsCount(LendProductStatus.Lending.GetHashCode());
        }

        public void SendToWarrantyDep(List<long> productIds, int employeeId)
        {
            if (productIds == null || productIds.Count == 0) return;
            _warrantyProductDao.SendToWarrantyDep(productIds, employeeId);
        }

        public void SendToTechnical(List<long> productIds, int employeeId)
        {
            if (productIds == null || productIds.Count == 0) return;
            _warrantyProductDao.SendToTechnical(productIds, employeeId);
            //_warrantyProductDao.UpdateProductStates(productIds, WarrantyProductStatus.AtTechnical.GetHashCode());
        }

        public void SendToSupplier(long id, int supplierId, string note, string sentDateString, string wishDateString, string dateFormat)
        {
            if (id <= 0) return;

            DateTime? sentDate, wishDate;
            DateTime dtSentDate, dtWishDate;
            if (DateTime.TryParseExact(sentDateString, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None,
                out dtSentDate))
            {
                sentDate = dtSentDate;
            }
            else
            {
                sentDate = DateTime.Today;
            }
            if (DateTime.TryParseExact(wishDateString, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None,
                out dtWishDate))
            {
                wishDate = dtWishDate;
            }
            else
            {
                wishDate = DateTime.Today.AddDays(7);
            }

            _warrantyProductDao.SendToSupplier(id, supplierId, note, sentDate, wishDate);
        }

        public void SendToSupplier(List<long> productIds, int supplierId, string note)
        {
            if (productIds == null || productIds.Count == 0) return;
            _warrantyProductDao.SendToSupplier(productIds, supplierId, note);
        }

        public void ReceivedBack(List<long> productIds)
        {
            if (productIds == null || productIds.Count == 0) return;

            _warrantyProductDao.UpdateLendProductStates(productIds, LendProductStatus.ReceivedBack.GetHashCode());
        }

        public void SendBackCustomer(List<long> productIds)
        {
            if (productIds == null || productIds.Count == 0) return;

            _warrantyProductDao.UpdateProductStates(productIds, WarrantyProductStatus.SentBackCustomer.GetHashCode());
        }

        public void SendToWarrantySupplier(List<long> productIds)
        {
            if (productIds == null || productIds.Count == 0) return;

            _warrantyProductDao.UpdateProductStates(productIds, WarrantyProductStatus.AtSupplier.GetHashCode());
        }

        public List<WarrantyTicketProductView> GetProcessingWarrantyProducts(int startIndex, int pageSize, string sorting)
        {
            return _warrantyProductDao.GetProcessingWarrantyProducts(startIndex, pageSize, sorting);
        }

        public int GetProcessingWarrantyProductsCount()
        {
            return _warrantyProductDao.GetProcessingWarrantyProductsCount();
        }

        public int GetProcessingWarrantyProductsCount(int employeeId)
        {
            return _warrantyProductDao.GetProcessingWarrantyProductsCount(employeeId);
        }

        public int GetWarrantyProductsAtSupplierCount()
        {
            return _warrantyProductDao.GetWarrantyProductsCount(WarrantyProductStatus.AtSupplier.GetHashCode());
        }

        public List<WarrantyTicketProductView> GetWarrantyProductsAtSupplier(int startIndex, int pageSize, string sorting)
        {
            return _warrantyProductDao.GetWarrantyTicketProducts(WarrantyProductStatus.AtSupplier.GetHashCode(), startIndex, pageSize, sorting);
        }

        public int GetWarrantyProductsAtTechnicalCount()
        {
            return _warrantyProductDao.GetWarrantyProductsCount(WarrantyProductStatus.AtTechnical.GetHashCode());
        }

        public int GetWarrantyProductsAtWarrantyDepCount()
        {
            return _warrantyProductDao.GetWarrantyProductsCount(WarrantyProductStatus.AtWarrantyDep.GetHashCode());
        }

        public List<WarrantyTicketProductView> GetWarrantyProductsAtTechnical(int startIndex, int pageSize, string sorting)
        {
            return _warrantyProductDao.GetWarrantyTicketProducts(WarrantyProductStatus.AtTechnical.GetHashCode(), startIndex, pageSize, sorting);
        }

        public List<WarrantyTicketProductView> GetWarrantyProductsAtWarrantyDep(int startIndex, int pageSize, string sorting)
        {
            return _warrantyProductDao.GetWarrantyTicketProducts(WarrantyProductStatus.AtWarrantyDep.GetHashCode(), startIndex, pageSize, sorting);
        }

        public WarrantyTicketProductView GetWarrantyTicketProductView(long id)
        {
            if (id <= 0) return null;
            return _warrantyProductDao.GetWarrantyTicketProductView(id);
        }

        public void UpdateProductStatus(long warrProductId, WarrantyProductStatus status)
        {
            if (warrProductId <= 0) return;

            _warrantyProductDao.UpdateProductStates(new List<long> { warrProductId }, status.GetHashCode());
        }

        public void UpdateProcessProduct(WarrantyTicketProductView productView)
        {
            _warrantyProductDao.UpdateProcessProduct(productView);
        }

        public List<WarrantyTicketProductView> GetProcessingWarrantyProducts(int employeeId, int startIndex, int pageSize, string sorting)
        {
            return _warrantyProductDao.GetProcessingWarrantyProducts(employeeId, startIndex, pageSize, sorting);
        }

        #endregion

        public void ReceivedCompleted(long id, int supplierId, string note, string sentDateString, string wishDateString, string receivedDateString, string dateFormat)
        {
            if (id <= 0) return;

            DateTime? sentDate = null, wishDate = null, receivedDate = null;
            DateTime dtSentDate, dtWishDate, dtReceivedDate;
            if (DateTime.TryParseExact(sentDateString, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None,
                out dtSentDate))
            {
                sentDate = dtSentDate;
            }
            if (DateTime.TryParseExact(wishDateString, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None,
                out dtWishDate))
            {
                wishDate = dtWishDate;
            }
            if (DateTime.TryParseExact(receivedDateString, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None,
                 out dtReceivedDate))
            {
                receivedDate = dtReceivedDate;
            }

            _warrantyProductDao.ReceivedCompleted(id, supplierId, note, sentDate, wishDate, receivedDate);
        }
    }
}
